﻿
Ext.namespace('LanWatch');
Ext.namespace('LanWatch.Grids');

var mainControllerLink = '/vsaPres/LanWatch/Main/';
var lanWatchImagesLink = '/vsaPres/Areas/LanWatch/Content/Images/';

//var mainControllerLink = '/LanWatch/Main/';
//var lanWatchImagesLink = '/Areas/LanWatch/Content/Images/';

Ext.define('Machine', {
    extend: 'Ext.data.Model',
    fields: ['Id', 'AgentGuid', 'AgentGuidStr', 'HostName', 'FullName', 'IsOnline', 'CurrentLogin',
             'ScanStatus', 'IsProbe', 'GroupName', 'PartitionId', 'MacAddress',
             'IPAddress', 'Ipv6Address', 'DefaultGateway', 'ConnectionGateway',
             'PrimaryWinsServer', 'SubnetMask', 'OsInfo', 'OSType', 'OsName',
             'HostName', 'OsGen', 'OsFamily', 'ImagePath', 'ScanRange',
             'Exclusions', 'DefaultSNMP', 'OwnerAgentGuid',
             'FormatedRange', 'Name', 'NetworkName', 'LastScanTime',
             'SNMPString', 'Probe', 'PartitionId', 'CurrentLogin', 'SnmpEnabled', 'ADEnabled', 'VProEnabled',
             'DiscoveredCount', 'ScannedCount']
});



Ext.define('LanWatch.Grids.LanWatchGridPanelBaseCls', {
    extend: 'LanWatch.Grids.GridPanelBaseCls',

    //Public Properties: 
    defaults: { menuDisabled: true },
    url: '',

    initComponent: function () {

        if (!this.columns) {
            this.columns = [
                { name: 'Id', header: 'Id', dataIndex: 'Id', hidden: true },
                {
                    header: '@~ ~@',
                    dataIndex: 'IsOnline',
                    hidden: false,
                    width: 50,
                    resizable: false,
                    renderer: renderOnlineStatusColumn
                },
               {
                   header: '@~ ~@',
                   editable: false,
                   name: 'Probe',
                   dataIndex: 'IsProbe',
                   width: 50,
                   renderer: function (value, metaData, record) {
                       //console.log(value);
                       var probeIcon = "";
                       if (value > 0) {
                           probeIcon = '<img style="margin-left:3px; margin-top:-1px;" src="/vsaPres/Areas/LanWatch/Content/Images/Network_Info.png" title="click to view discovered devices by this probe"' +
                                                    ' onmouseover="this.style.cursor=\'pointer\'" onclick="window.open(\'/vsapres/LanWatch/Main/LanWatchProbe?agentGuid=' + String(record.data.AgentGuid) + '&displayName=' + String(record.data.FullName) + '\');">';
                       }
                       //console.log(probeIcon);
                       return probeIcon;
                   }
               },
                {
                    header: '@~Name~@',
                    dataIndex: 'FullName',
                    width: 270,
                    filterable: true
                },
                { header: '@~IP Address~@', name: 'IP Address', dataIndex: 'IPAddress', autoWidth: true, filterable: true },
                { header: '@~Mac Address~@', name: 'MAC Address', dataIndex: 'MacAddress', autoWidth: true },
                { header: '@~Default Gateway~@', name: 'Gateway IP', dataIndex: 'DefaultGateway', width: 100, hidden: false },
                { header: '@~Network Name~@', name: 'NetworkName', dataIndex: 'NetworkName', autoWidth: true },
                {
                    header: '@~Scan Status~@',
                    name: 'Scan Status',
                    dataIndex: 'ScanStatus',
                    autoWidth: true,
                    width: 200,
                    renderer: function (value, metaData, record) {

                        if (record.data.ScanStatus.indexOf("Deep Scan") != -1) {

                            var devicesFound = record.data.DiscoveredCount;
                            var devicesScanned = record.data.ScannedCount;
                            probeStatus = devicesScanned / devicesFound;

                            var id = Ext.id();

                            Ext.Function.defer(function (scanStatus, id) {
                                var bar = Ext.create('Ext.ProgressBar', {
                                    renderTo: id,
                                    value: probeStatus,
                                    text: Math.round(probeStatus * 100) + '@~% completed...~@'
                                });
                            }, 25, this, [probeStatus, id]);

                            return '<div id="' + id + '"></div>';

                        } else {
                            return record.data.ScanStatus;
                        }

                    }

                },
                { header: '@~Scan Range~@', name: 'Scan Range', dataIndex: 'ScanRange', autoWidth: true },
                {
                    header: '@~Last Scan Time~@',
                    name: 'LastScanTime',
                    dataIndex: 'LastScanTime',
                    autoWidth: true,
                    renderer: function (value, metaData, record) {
                        if (record.data.LastScanTime == null)
                            return "";
                        date = new Date(parseInt(record.data.LastScanTime.substr(6)));
                        //return date.toLocaleString();
                        return Ext.Date.format(date, 'H:i:s a Y-m-d');
                    }
                },
                {
                    hidden: true,
                    name: 'agentGuid',
                    header: '@~Agent Guid~@',
                    dataIndex: 'AgentGuid',
                    filterable: true
                },


                { header: '@~Partition Id~@', name: 'PartitionId', dataIndex: 'PartitionId', hidden: true },
                { header: '@~Host Name~@', name: 'Host Name', dataIndex: 'HostName', hidden: true },
                { header: 'Group', name: 'Group', dataIndex: 'GroupName', hidden: true },

                { header: '@~Default Gateway~@', name: 'Default Gateway', dataIndex: 'DefaultGateway', width: 100, hidden: true },

                { header: '@~OS Type~@', name: 'OS Type', dataIndex: 'OSType', hidden: true },
                { header: '@~OS Name~@', name: 'OS Name', dataIndex: 'OsName', hidden: true}];

        } // End of if (!this.columns) 

        if (this.extraColumns) {
            for (var i = 0; i < this.extraColumns.length; i++) {
                this.columns.push(this.extraColumns[i]);
            }
        }
        this.store = this.buildStore(this.model, this.idProperty, this.totalProperty);        
        this.selModel = Ext.create('Ext.selection.CheckboxModel', {
            mode: 'MULTI'
        });

        this.callParent(arguments);
    }, // end of initComponont

    //buildStore:--------------------------------------------------------------
    buildStore: function (model, idProperty, totalProperty) {
        var dataRoot = this.root;
        var store = Ext.create('Ext.data.JsonStore', {
            model: model,
            proxy: {
                type: 'ajax',
                url: this.url,
                root: this.root,
                reader: { type: 'json', root: dataRoot, totalProperty: totalProperty, idProperty: idProperty }
            },
            autoLoad: false,
            listeners: {
                update: function (store, record, operationType, eOpts) {
                    for (var i = 0, len = record.modified.length; i < len; i++) {
                        alert(record.modified[i]);
                    }

                    //debugger;
                    //commit to the database
                }
            }
        });
        return store;
    }, // End of buildStore.

    loadData: function (data) {
        this.store.loadData(data);
    },

    load: function () {
        return this.store.load();
    },

    getStore: function () {
        return this.store;
    }


});                           //End of define    
  